package service;

import dao.CourseDao;

import java.util.List;
import java.util.Map;

import dao.CommonDAO;
import entity.Characters;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CharacterService {
    private CourseDao dao = new CourseDao();
    private CommonDAO cd=new CommonDAO();

    public String getCharacterNameById(int characterId){
        return dao.getCharacterNameById(characterId);

    }

    public List<Characters> adminCharacters(){
        List<Characters> list=new ArrayList<>();
        String sql="select * from characters";
        List<Map<String,Object>> mapList = cd.executeQuery(sql, new Object[0]);

        for(int i=0;i<mapList.size();i++){
            Characters cha=new Characters();
            Map<String, Object> tempMap = mapList.get(i);
            cha.setCharacterID(Integer.parseInt(tempMap.get("characterID").toString()));
            cha.setCharacterName(tempMap.get("cname").toString());

            list.add(cha);
        }
        return list;
    }

    public int delete(Integer id){
        String sql="delete from characters where id=?";
        int count=cd.executeUpdate(sql, new Object[]{id});
        return count;
    }
    public int insert(Integer characterID,String cname){
        String sql="insert(characterId,cname) into characters values(?,?)";
        int count=cd.executeUpdate(sql,new Object[]{characterID,cname});
        return count;
    }
    public int update(Integer characterId,String cname){
        String sql="update characters set cname=? where characterID=?";
        int count=cd.executeUpdate(sql,new Object[]{cname,characterId});
        return count;
    }
    public List<Characters> charactersList(Integer characterID){
        List<Characters> list=new ArrayList<>();
        String sql="select * from characters where characterID=?";
        List<Map<String,Object>> mapList = cd.executeQuery(sql, new Object[]{characterID});
        for(int i=0;i<mapList.size();i++){
            Characters cha=new Characters();
            Map<String, Object> tempMap = mapList.get(i);
            cha.setCharacterID(Integer.parseInt(tempMap.get("characterID").toString()));
            cha.setCharacterName(tempMap.get("cname").toString());

            list.add(cha);
        }
        return list;
    }
}
